package com.qf.rabbitmq2203.config;
import com.qf.rabbitmq2203.constants.Constants;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMqConfig {

    /**
     * 配置RabbitTemplate
     * @param connectionFactory
     * @return
     */
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){

        RabbitTemplate rabbitTemplate = new RabbitTemplate();

        //设置连接工厂对象
        rabbitTemplate.setConnectionFactory(connectionFactory);

        //设置returncallback

        return rabbitTemplate;
    }

    /**
     * 配置交换机A
     * @return
     */
    @Bean("exchangeA")
    public TopicExchange topicExchangeA(){

        TopicExchange exchangeA = new TopicExchange(Constants.ExchangeA, true, false);

        return exchangeA;
    }

    /**
     * 配置交换机B
     * @return
     */
    @Bean("exchangeB")
    public TopicExchange topicExchangeB(){

        TopicExchange ExchangeB = new TopicExchange(Constants.ExchangeB, true, false);

        return ExchangeB;
    }

    /**
     * 配置死信交换机
     * @return
     */
    @Bean
    public DirectExchange directExchange(){

        DirectExchange directExchange = new DirectExchange(Constants.DEAD_LETTER_EXCHANGE_NAME, true, false);

        return directExchange;
    }


    /**
     * 配置延迟队列A
     * @return
     */
    @Bean("delayQueueA")
    public Queue delayQueueA(){
        Map<String,Object> args=new HashMap<>();
        args.put("x-dead-letter-exchange", Constants.DEAD_LETTER_EXCHANGE_NAME);
        args.put("x-dead-letter-routing-key",Constants.DEAD_LETTER_QUEUE_10S_ROUTINGKEY);
        args.put("x-message-ttl",10000);
        return QueueBuilder.durable(Constants.DELAY_QUEUE_A).withArguments(args).build();
    }

    /**
     * 配置延迟队列B
     * @return
     */
    @Bean("delayQueueB")
    public Queue delayQueueB(){
        Map<String,Object> args=new HashMap<>();
        args.put("x-dead-letter-exchange", Constants.DEAD_LETTER_EXCHANGE_NAME);
        args.put("x-dead-letter-routing-key",Constants.DEAD_LETTER_QUEUE_20S_ROUTINGKEY);
        args.put("x-message-ttl",20000);
        return QueueBuilder.durable(Constants.DELAY_QUEUE_B).withArguments(args).build();
    }

    /**
     * 死信队列A
     * @return
     */
    @Bean("deadQueueA")
    public Queue deadQueueA(){

        Queue queue = new Queue(Constants.DEAD_LETTER_QUEUE_A);

        return queue;
    }

    /**
     * 死信队列B
     * @return
     */
    @Bean("deadQueueB")
    public Queue deadQueueB(){

        Queue queue = new Queue(Constants.DEAD_LETTER_QUEUE_B);

        return queue;
    }

    /**
     * exchangeA 与 delayQueueA 绑定
     */
    @Bean
    public Binding binding1(TopicExchange exchangeA, Queue delayQueueA){

        Binding binding = BindingBuilder.bind(delayQueueA).to(exchangeA).with(Constants.ROUNTINGKEYORANGE);
        return binding;
    }

    /**
     * exchangeB 与 delayQueueB 绑定
     */
    @Bean
    public Binding binding2(TopicExchange exchangeB, Queue delayQueueB){

        Binding binding = BindingBuilder.bind(delayQueueB).to(exchangeB).with(Constants.ROUNTINGKEYBLUE);
        return binding;
    }


    /**
     * directExchange 与 deadQueueA 绑定
     */
    @Bean
    public Binding binding3(DirectExchange directExchange, Queue deadQueueA){

        Binding binding = BindingBuilder.bind(deadQueueA).to(directExchange).with(Constants.DEAD_LETTER_QUEUE_10S_ROUTINGKEY);
        return binding;
    }

    /**
     * directExchange 与 deadQueueB 绑定
     */
    @Bean
    public Binding binding4(DirectExchange directExchange, Queue deadQueueB){

        Binding binding = BindingBuilder.bind(deadQueueB).to(directExchange).with(Constants.DEAD_LETTER_QUEUE_20S_ROUTINGKEY);
        return binding;
    }


}
